package com.gupt.youhd.util;/*
@author name
@data 2023/6/9-12:55
*/

/**
 * 用户从redis获取用户信息
 */

import cn.hutool.json.JSONUtil;
import com.gupt.youhd.common.StringConstant;
import com.gupt.youhd.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
@Component
public class YouHDUtils {

    @Autowired
    private RedisUtils redisUtils;

    //校验验证码规范
    public static boolean isMobileNumberValid(String mobileNumber) {
        if (mobileNumber == null || mobileNumber.length() == 0) {
            return false;
        }
        String regex = "^1[3-9]\\d{9}$";
        return mobileNumber.matches(regex);
    }

    //根据请求头的token获取用户id
    public User getUser(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String s = redisUtils.get(StringConstant.LOGIN_ADMIN_INFO_TOKEN + token);
        //将用户信息的json数据 转成对象
        User user = JSONUtil.toBean(s, User.class);
        return  user;
    }
}
